<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<jsp:include page="../common/import.jsp" flush="true"></jsp:include>
<body>
<%-- <jsp:include page="../common/header.jsp" flush="true"></jsp:include> --%>
<div class="header">
	<div class="container clear">
    	<a class="fl" href="${base}/agent/merchant/merchantList.xhtml"><img src="http://www.wangpos.com/public/images/logo.png" alt="旺POS"></a>
        <a class="fl cfff f16" href="${base}/agent/merchant/merchantList.xhtml">&#8226;&nbsp;代理商平台</a>
        <a class="fr cfff" href="${base}/agent/user/logout.xhtml">退出</a>
        <a class="fr cfff mr10" href="${base}/agent/user/personalCenter.xhtml"><span class="iconfont mr5">&#xf012d;</span>${user.username}</a>
        <%-- 备用：显示用户等级
        <a class="fr cfff mr10" href="javascript:">${user.username}</a> 
        --%>
    </div>
</div>


<div class="bgfff p20">
	<div class="container clear min-h500 pb100">
        <ul class="clear tc f18 mt20 add-store-ul">
        	<li class="fl lihover">搜索门店</li>
            <li class="fl">填写信息</li>
            <li class="fl">预览并提交</li>
        </ul>
        
        <p class="mt30">通过门店搜索进行快速添加</p>
        <input type="hidden" id="qualificationId" value="${qualificationId}">
        <input type="hidden" id="lastId"/>
		<input type="hidden" id="target"/>
		<input type="hidden" id="pageIndex" value="1"/>
		<input type="hidden" id="size"/>
		
        <div class="mt10 max-h">
        	<select id="province" name="province" class="b w200 lg40">
					<option>北京市</option>
					<option>天津市</option>
					<option>河北省</option>
					<option>吉林省</option>
					<option>上海市</option>
					<option>山西省</option>
					<option>内蒙古</option>
					<option>辽宁省</option>
					<option>黑龙江省</option>
					<option>江苏省</option>
					<option>浙江省</option>
					<option>安徽省</option>
					<option>福建省</option>
					<option>江西省</option>
					<option>山东省</option>
					<option>河南省</option>
					<option>湖北省</option>
					<option>湖南省</option>
					<option>广东省</option>
					<option>广西</option>
					<option>海南省</option>
					<option>重庆市</option>
					<option>西藏</option>
					<option>宁夏</option>
					<option>四川省</option>
					<option>贵州省</option>
					<option>云南省</option>
					<option>陕西省</option>
					<option>甘肃省</option>
					<option>青海省</option>
					<option>新疆</option>
			</select> 
            <select id="city" name="city" class="b w200 lg40">
                <!-- <option>朝阳区</option> -->
                <option>北京市</option>
                <option>北京市辖县</option>
            </select>
            <input class="w600 p10 h20 ml20" type="text" placeholder="搜索门店名称或mcode" id="shopName"  onkeypress="CheckInfo(event)">
            <button class="p10 bg1eb9ef cfff ml20 b-radius w100 h20 b" onclick="search()">搜索</button>
        </div>
        
        <ul id="store_list" class="clear">
        	<!-- 
        	<li class="w380 fl h200 b mr10 mt10">
            	<div class="p10">
                	<div class="h100 bb" style="border-bottom-style:dashed">
                    	<p>肯德基(T3店)</p>
                        <p>联系电话：(010)64532716</p>
                        <p>店铺地址：北京朝阳区首都机场3号航站楼4楼(近四经路)</p>
                    </div>
                    
                    <p class="mt10 c1eb9ef">添加门店时请补全信息</p>
                    <button class="p10 w150 bgac1 b-radius cfff mt5">添加新门店</button>
                </div>
            </li>
            <li class="w380 fl h200 b mr10 mt10">
            	<div class="p10">
                	<div class="h100 bb" style="border-bottom-style:dashed">
                    	<p>肯德基(T3店)</p>
                        <p>联系电话：(010)64532716</p>
                        <p>店铺地址：北京朝阳区首都机场3号航站楼4楼(近四经路)</p>
                    </div>
                    
                    <p class="mt10 c1eb9ef">添加门店时请补全信息</p>
                    <button class="p10 w150 bgac1 b-radius cfff mt5">添加新门店</button>
                </div>
            </li>
            <li class="w380 fl h200 b mr10 mt10">
            	<div class="p10">
                	<div class="h100 bb" style="border-bottom-style:dashed">
                    	<p>肯德基(T3店)</p>
                        <p>联系电话：(010)64532716</p>
                        <p>店铺地址：北京朝阳区首都机场3号航站楼4楼(近四经路)</p>
                    </div>
                    
                    <p class="mt10 c1eb9ef">添加门店时请补全信息</p>
                    <button class="p10 w150 bgac1 b-radius cfff mt5">添加新门店</button>
                </div>
            </li>
            <li class="w380 fl h200 b mr10 mt10">
            	<div class="tc mt20">
                	<button class="iconfont caac81a" style="font-size:100px">&#xf0175;</button>
                    <p class="c1eb9ef">手动d店铺</p>
                </div>
            </li> 
            -->
        </ul>
       <!--   <ul class="store_list"></ul> -->
    </div>
</div>

<jsp:include page="../common/footer.jsp" flush="true"></jsp:include>

<script type="text/javascript">
$(function(){
	var edit_city = '';
	$("#province").change(function () {
		var html = "<option value=''>城市</option>";
		$("#city").html(html);
		
		var prov = "prov";
		var value = $(this).val();
		var url = "http://www.wangpos.com/service/address/refreshCity";
		var data = {"key": prov,"value":value,"url":url};
        $.ajax({
			url: "${base}/agent/agent/authorZone.xhtml",
			type: "POST",
			data: data,
			dataType: "JSON",
			success: function (data) {
				var ison = data +";";
				if (data.status == 0) {
					hintError("数据错误");
					return;
				}
				var list = data.data;
				html = ''
				for (var i = 0; i < list.length; i++) {
					html += '<option value="' + list[i] + '">' + list[i] + '</option>';
				}
				// $("#prov").val(val);
				$("#city").html(html);
				if(edit_city != ''){
					$('#city').val(edit_city);
				}
				$('#city').change();
			},
			error: function (data) {
				alert('服务端报错' + data.status);
			}
		}); 
    });
});

//搜索按钮
 function search() {
    	if($("#shopName").val() == ""){
    		return false;
    	}
    	
    	init();	
        
        var addByHand = "";
        addByHand += "<li id='addByHand' class='w380 fl h200 b mr10 mt10'>";
        addByHand += "<div class='tc mt20' onclick='addShopByHand()'>";
        addByHand += "<button class='iconfont caac81a' style='font-size:100px'>&#xf0175;</button>";
        addByHand += "<div class='card_store_main3'>";
        addByHand += "<p class='c1eb9ef'>手动添加店铺</p>";
        addByHand += "</div>";
    	addByHand += "</li>";
    	
    	$("#store_list").html(addByHand);
    	
    	var city = $("#city").val();
    	var shopName = $("#shopName").val();
        var lastId = $("#lastId").val();//""
        var pageIndex = $("#pageIndex").val();//1,size=9,init方法中
        
        city = city.replace(/市/,"");
        
    	doSearch(city, shopName, lastId, pageIndex, 20);
    }
    
    //searchByAdmin
	function doSearch(city, shopName, lastId, pageIndex, limit){
         //searchByPosAdmin
		$.ajax({
			url: "${base}/agent/store/doSearch.xhtml",
			type: "POST",
            data: {
           	 "city": city,
           	 "shopName":shopName,
           	 "lastId": lastId,
           	 "pageIndex":pageIndex,
           	 "limit": limit
          	},
            dataType: "json",
            async : false,
            success: function (data) {
				if(data.status == 0){
					var mlist = data.data;
					var mcodeDlag = data.info;
					for(var i=0; i < mlist.length; i++){
						var from = mlist[i].from;
						$("#target").val(from);
						
						if(from == 'soa'){
							$("#lastId").val(mlist[i].storeId);
						}
						
						var html = "<li class='w380 fl h200 b mr10 mt10'>";
           			 	html += "<div class='p10'>";
                        html += "<div class='h100 bb' style='border-bottom-style:dashed'>";
                        html += "<p>"+mlist[i].name+"</p>";    
                        html += "<p>";
                        if(from == 'soa' || mcodeDlag == 'mcode'){
                        	html += "联系电话：";
                        	if (mlist[i].tel!=null&&mlist[i].tel!='') {
								html+=mlist[i].tel;
							}
                        }else{
                        	html += "联系电话：";
                        	if (mlist[i].telephone!=null&&mlist[i].telephone!='') {
								html+=mlist[i].telephone;
							}
                        }
                        html += "</p>";
                        html += "<p>";
                        html += "店铺地址：";
                        var address =  mlist[i].address;
                        if (!address==undefined&&address!='') {
                        	if(from == 'soa' || mcodeDlag == 'mcode'){
	                        	if(mlist[i].address.length > 30){
	                           	 	html += address.address_detail.substring(0, 29)+"...";
	                            }else{
	                           	 	html += address.address_detail;
	                            }
	                        }else{
	                        	if(mlist[i].address.length > 30){
	        						html+=address.substring(0, 29)+"...";
	        					}else{
	        						html+=address;
	        					}
	                        }
						}else{
							html+="";
						}
	                        
                        html += "</p>";
                        html += "</div>";
                       
                        if(mlist[i].existFlag == 1){
    						html+="<div id='import_"+i+"' >";
    						if(mlist[i].owned == 0){
    							html+="<p class='mt10 c1eb9ef'>已存在旺POS体系可直接添加</p>";
    							html += "<button class='p10 w150 bgac1 b-radius cfff mt5' onclick='bindUserMerchant(\""+i+"\",\""+mlist[i].storeId+"\",\""+mlist[i].mcode+"\")'>添加新门店</button>";
    						}else{
    							html+="<p class='mt10 c1eb9ef'>门店已存在，并被其他商户认领</p>";
    						}
    						html+="</div>";
    					}else{
    						html+="<p class='mt10 c1eb9ef'>添加门店时请补全信息</p>";
    						html+="<button class='p10 w150 bgac1 b-radius cfff mt5' onclick='addByBaidu(\""+mlist[i].uid+"\", this)'>添加新门店</button>";
    					}  
                        html += "</div>";
                        html += "</li>";
                        
						$("#addByHand").before(html);
					}
					if(mlist.length = 8){
						$("#loadMerchant").css("display","block");
					}else{
						$("#loadMerchant").css("display","none");
					}
				}else{
					//dangerHint(data.info);
					//location.href= "${absUrl}merchant/index";
				}
			}
		});
	}
 
function CheckInfo(e){
	    	/* var key = window.event ? e.keyCode:e.which;
	    	
	        var keychar = String.fromCharCode(key); */
	    	 if(e.keyCode == 13){
	         	search();
	         }
	    }
//手动添加
function addShopByHand() {
    	if(window.sessionStorage){
    		var s = window.sessionStorage;
			s.removeItem('baidu');
    	}
    	var qualificationId = $("#qualificationId").val();
        window.location = "${base}/agent/store/addByBaidu.xhtml?qualificationId="+qualificationId;
    }
function addByBaidu(uid, obj) {
	/* if(window.sessionStorage){
		var s = window.sessionStorage;
		//var s = window.localStorage;
		s.setItem('baidu', JSON.stringify(baiduMerchant));
		//sessionStoreage.setItem('baidu',baiduMerchant);
		$(obj).removeClass().addClass("btn_3_hover");
		//var uid=JSON.stringify(baiduMerchant).uid;
		//window.location.href = "${absUrl}merchant/addByBaidu";
	} */
		//alert(uid);
		var qualificationId = $("#qualificationId").val();
		window.open("${base}/agent/store/addByBaidu.xhtml?qualificationId="+qualificationId+"&uid="+uid);
}
//已在旺pos体系的，直接进行绑定操作
function bindUserMerchant(i, storeId, mcode) {
	var qualificationId = $("#qualificationId").val();
    $.ajax({
        url: "${base}/agent/store/bind.xhtml",
        type: "POST",
        dataType: "json",
        data:{
        	storeId:storeId,
        	mcode:mcode,
        	qualificationId:qualificationId,
        	claimStatus:1
        },
        success: function (data) {
            if (data.status == 0) {
            	//var userType = ${userType};
            	var h = $('#import_'+i);
            	h.html('<p>门店已添加，等待审核</p>');
            	/* if(userType == 2){
            		//alert(userType);
            		window.location.href = window.localUrl + "account/microChooseMer";//选择商户类型
            	} */
            } else {
            	//dangerHint(data.info);
                $("#submitBtn").val('提交申请');
                $("#submitBtn").prop({disabled: false});
            }
        },
        error: function (data) {
            alert('服务端报错' + data.status);
            $("#submitBtn").val('提交申请');
            $("#submitBtn").prop({disabled: false});
        }

    });
}

$("#loadMerchant").click(function(){
	var target = $("#target").val();
	if(target == "baidu"){
		$("#pageIndex").val(parseInt($("#pageIndex").val())+1);
	}
	
	var city = $("#city").val();
	var shopName = $("#shopName").val();
    var lastId = $("#lastId").val();
    var pageIndex = $("#pageIndex").val();
    
    city = city.replace(/市/,"");
    
    doSearch(city, shopName, lastId, pageIndex, 21);
});

function init(){
	$("#loadMerchant").css("display","block");
	$("#lastId").val("");
	$("#target").val("");
	$("#pageIndex").val("1");
	$("#size").val("9");
}
</script>
</body>
</html>